package com.admin.controller;


import com.admin.entity.Announcement;
import com.admin.service.AnnouncementService;
import com.admin.utils.Result;
import com.admin.vo.query.AnnouncementQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 公告通知表 前端控制器
 * </p>
 *
 * @author szy
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    /*分页查询*/
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:announcement:select')")
    public Result list(AnnouncementQueryVo announcementQueryVo){
        IPage<Announcement> page = new Page<Announcement>(announcementQueryVo.getPageNo(),announcementQueryVo.getPageSize());
        announcementService.getAnnountByPage(page,announcementQueryVo);
        return Result.ok(page);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:announcement:add')")
    public Result add(@RequestBody Announcement announcement){
        if (announcementService.save(announcement)){
            return Result.ok().message("添加成功");
        }else
            return Result.error().message("添加失败");
    }

    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:announcement:edit')")
    public Result update(@RequestBody Announcement announcement){
        if (announcementService.updateById(announcement)){
            return Result.ok().message("修改成功");
        }else
            return Result.error().message("修改失败");
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:announcement:delete')")
    public Result delete(@PathVariable Integer id){
        if (announcementService.removeById(id)){
            return Result.ok().message("删除成功");
        }else
            return Result.error().message("删除失败");
    }
}

